package com.wuji1626.spark.rdd.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import java.text.SimpleDateFormat
import java.util.Locale

object RDD_Transform_groupBy_LogPV {

  def main(args: Array[String]): Unit = {
    // Step1: 准备环境
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc = new SparkContext(sparkConf)


    // Step2: 算子 groupBy
    val rdd = sc.textFile("datas/apache.log")
    var timeRDD = rdd.map(
      line => {
        val data = line.split(" ")
        val time = data(3)
        val sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.US)
        val date = sdf.parse(time)
        val sdf1 = new SimpleDateFormat("HH")
        val hour = sdf1.format(date)
        (hour, 1)
      }
    ).groupBy(_._1)
    timeRDD.map{
      case (hour, iter) => {
        (hour, iter.size)
      }
    }.collect.foreach(println)

    // Step3: 关闭环境
    sc.stop()
  }

}
